/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.domain.account.action;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.account.core.domain.account.Account;
import info.peigen.hotpot.business.account.core.domain.account.AccountFreezeLog;
import info.peigen.hotpot.business.account.core.domain.account.AccountLog;
import info.peigen.hotpot.business.account.core.domain.account.AccountTransLog;
import info.peigen.hotpot.business.account.core.repository.entity.AccountEntity;
import info.peigen.hotpot.business.account.core.repository.entity.AccountFreezeLogEntity;
import info.peigen.hotpot.business.account.core.repository.entity.AccountLogEntity;
import info.peigen.hotpot.business.account.core.repository.entity.AccountTransLogEntity;
import info.peigen.hotpot.business.account.facade.info.AccountInfo;
import info.peigen.hotpot.business.account.facade.info.AccountLogInfo;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <b>(AccountConvertor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/12
 */
public class AccountConvertor {
    public static AccountEntity convertAccount(Account domain) {
        AccountEntity entity = BeanUtil.copyProperties(domain, AccountEntity.class);

        if (ObjectUtil.isNotNull(domain.getCode())) {
            entity.setChannelCode(domain.getCode());
        }
        return entity;
    }

    public static Account convertAccount(AccountEntity entity) {
        Account account = new Account();
        BeanUtil.copyProperties(entity, account);
        account.getBalance().setCurrency(entity.getCurrency());
        account.getFreezeAmount().setCurrency(entity.getCurrency());
        account.getTransitAmount().setCurrency(entity.getCurrency());
        account.getCreditQuotaAmount().setCurrency(entity.getCurrency());
        account.getHistoryIncomeAmount().setCurrency(entity.getCurrency());
        account.getHistoryPaidAmount().setCurrency(entity.getCurrency());

        if (ObjectUtil.isNotNull(entity.getChannelCode())) {
            account.setCode(entity.getChannelCode());
        }
        return account;
    }

    public static AccountLogEntity convertAccountLog(AccountLog domain) {
        AccountLogEntity entity = BeanUtil.copyProperties(domain, AccountLogEntity.class, "account")
                .setBalance(Objects.nonNull(domain.getBalance()) ? domain.getBalance() : domain.getAccount().getBalance())
                .setBalanceCurrency(Objects.nonNull(domain.getBalance()) ? domain.getBalance().getCurrency() : domain.getAccount().getCurrency())
                .setTransAmountCurrency(domain.getTransAmount().getCurrency());
        entity.setAccountId(domain.getAccount().getAccountId());
        return entity;
    }

    public static AccountTransLogEntity convertAccountTransLog(AccountTransLog domain) {
        return BeanUtil.copyProperties(domain, AccountTransLogEntity.class, "debitAccountLog,creditAccountLog")
                .setDebitAccountId(domain.getDebitAccountLog().getAccount().getAccountId())
                .setCreditAccountId(domain.getCreditAccountLog().getAccount().getAccountId())
                .setDebitAccountLogId(domain.getDebitAccountLog().getAccountLogId())
                .setCreditAccountLogId(domain.getCreditAccountLog().getAccountLogId())
                .setFeePayerAccountLogId(ObjectUtil.isNotNull(domain.getFeePayerAccountLog()) ? domain.getFeePayerAccountLog().getAccountLogId() : null)
                .setFeePayeeAccountLogId(ObjectUtil.isNotNull(domain.getFeePayeeAccountLog()) ? domain.getFeePayeeAccountLog().getAccountLogId() : null)
                .setTransAmountCurrency(domain.getTransAmount().getCurrency());
    }

    public static AccountTransLogEntity convertAccountSingleTransLog(AccountTransLog domain) {
        return BeanUtil.copyProperties(domain, AccountTransLogEntity.class, "debitAccountLog,creditAccountLog")
                .setDebitAccountId(domain.getDebitAccountLog().getAccount().getAccountId())
                .setDebitAccountLogId(domain.getDebitAccountLog().getAccountLogId())
                .setFeePayerAccountLogId(ObjectUtil.isNotNull(domain.getFeePayerAccountLog()) ? domain.getFeePayerAccountLog().getAccountLogId() : null)
                .setFeePayeeAccountLogId(ObjectUtil.isNotNull(domain.getFeePayeeAccountLog()) ? domain.getFeePayeeAccountLog().getAccountLogId() : null)
                .setTransAmountCurrency(domain.getTransAmount().getCurrency());
    }

    public static AccountFreezeLogEntity convertAccountFreezeLog(AccountFreezeLog domain) {
        AccountFreezeLogEntity entity = new AccountFreezeLogEntity();
        BeanUtil.copyProperties(domain, entity);
        return entity;
    }

    public static AccountInfo convertAccountInfo(Account account) {
        return BeanUtil.copyProperties(account, AccountInfo.class);
    }

    public static AccountLog convertAccountLog(AccountLogEntity accountLogEntity) {
        AccountLog accountLog = BeanUtil.copyProperties(accountLogEntity, AccountLog.class);
        accountLog.getTransAmount().setCurrency(accountLogEntity.getTransAmountCurrency());
        accountLog.getBalance().setCurrency(accountLogEntity.getBalanceCurrency());
        return accountLog;
    }

    public static AccountLogInfo convertAccountLogInfo(AccountLog accountLog) {
        return BeanUtil.copyProperties(accountLog, AccountLogInfo.class);
    }

    public static AccountFreezeLogEntity convertAccountFreezeLogEntity(AccountFreezeLog accountFreezeLog) {
        return BeanUtil.copyProperties(accountFreezeLog, AccountFreezeLogEntity.class);
    }

    public static AccountInfo convertAccountInfo(AccountEntity entity) {
        return BeanUtil.copyProperties(entity, AccountInfo.class);
    }

    public static List<AccountInfo> convertAccountInfos(List<AccountEntity> entities) {
        return entities.stream().map(entity -> convertAccountInfo(entity)).collect(Collectors.toList());
    }
}